﻿using System;
using System.Collections.Generic;
using System.IO;

namespace 栅格数据结构与算法
{
    struct RunLengthEncodingNode
    {
        public int row;
        public int pixelValue;
        public int count;
    }

    /// <summary>
    /// 游程栅格编码
    /// </summary>
    /// <remarks>数据结构：线性表</remarks>
    class RunLengthEncoding : Raster
    {
        public List<RunLengthEncodingNode> runLengthEncodings;

        public RunLengthEncoding()
        {
            runLengthEncodings = new List<RunLengthEncodingNode>();
        }

        public void ReadDataFromFile(string path)
        {
            string[] allLinesArray;
            try
            {
                allLinesArray = File.ReadAllLines(path);
                Rows = allLinesArray.Length;
                for (int i = 0; i < Rows; i++)
                {
                    int colCount = 0;
                    string[] lineArray = allLinesArray[i].Split(' ');
                    for (int j = 0; j < lineArray.Length; j++)
                    {
                        RunLengthEncodingNode runLengthEncodingNode;
                        string[] node = lineArray[j].Split(',');
                        runLengthEncodingNode.row = i;
                        runLengthEncodingNode.pixelValue = int.Parse(node[0]);
                        runLengthEncodingNode.count = int.Parse(node[1]);
                        if (i == 0)
                            colCount += runLengthEncodingNode.count;
                        runLengthEncodings.Add(runLengthEncodingNode);
                    }
                    if (Cols == 0)
                        Cols = colCount;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }

        public void WriteRunLengthEncoding()
        {
            int row = 0;
            foreach (var item in runLengthEncodings)
            {
                if (item.row > row)
                {
                    Console.WriteLine();
                    row = item.row;
                }
                Console.Write($"{item.pixelValue},{item.count} ");
            }
            Console.WriteLine("\n-----------------------");
        }

        public void ToRaster()
        {
            try
            {
                int row = 0;
                int col = 0;
                if (PixelValue == null)
                    PixelValue = new int[Rows, Cols];
                for (int i = 0; i < runLengthEncodings.Count; i++)
                {
                    if (runLengthEncodings[i].row == row)//当前行
                    {
                        for (int j = col; j < col + runLengthEncodings[i].count; j++)
                        {
                            PixelValue[row, j] = runLengthEncodings[i].pixelValue;
                        }
                        col += runLengthEncodings[i].count;
                    }
                    else  //下一行
                    {
                        row = runLengthEncodings[i].row;
                        col = 0;
                        for (int j = col; j < col + runLengthEncodings[i].count; j++)
                        {
                            PixelValue[row, j] = runLengthEncodings[i].pixelValue;
                        }
                        col += runLengthEncodings[i].count;
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
    }
}
